﻿using DBModels;
using Models.RModels;
using RedisHelper.Article;
using RedisHelper.Notify;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.WDS.Methods
{
    public class InteractiveMet
    {
        /// <summary>
        /// 文章点赞
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="index"></param>
        /// <returns>1:点赞成功,2:取消点赞成功,3:失败</returns>
        public int ArticleLike(int uid, int articleId)
        {
            var like = new Like();
            //查询当前文章用户是否点赞
            if (like.IsArticleLike(articleId, uid))
            {
                //取消
                if (like.DelArticleLike(articleId, uid))
                    return 2;
            }
            else
            {
                //插入新点赞的消息表
                Task.Run(() =>
                {
                    //获取对象用户编号
                    using (ArticeContext context = new ArticeContext())
                    {
                        var curuid = context.ArticleLists.FirstOrDefault(u => u.Id == articleId).ReleaseId;
                        new LatestLike().InsertLastestLike(curuid, new R_LatestLikes()
                        {
                            LikeId = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                            UserId = uid,
                            IsClassification = 0,
                            ArticleId = articleId,
                            CommentId = "0"
                        });
                    }
                });
                    
                //点赞
                if (like.InsertArticleLike(articleId, uid))
                    return 1;
            }
            return 3;
        }
    }
}
